<?php

class DashboardController extends SilkControllerBase {
	function index($params) {
		if(!isset($_SESSION["silk_user"]->params["id"])) {
			$link = SilkResponse::create_url(array(	"controller" => "homepage",
													"action" => "index"
													));
			redirect($link);
		}
		$user = MpController::get_user($_SESSION["silk_user"]->params["id"]);
		$this->set("manageLinks", $this->buildPortfolioLinks($params, true, false));
		$this->set("createLinks", $this->buildPortfolioLinks($params, false, true));
	}
	
	function showMyActivity($params) {
		if(!isset($_SESSION["silk_user"]->params["id"])) {
			$link = SilkResponse::create_url(array(	"controller" => "homepage",
													"action" => "index"
													));
			redirect($link);
		}
		
		if(!isset($params["div"])) $params["div"] = "MyActivity";
		$user = MpController::get_user($params["user_id"]);
		$this->set("user", $user);

		$portfolios = $user->get_portfolios();
		$this->set("portfolioCount", count($portfolios));
		$this->set("portfolios", $portfolios);
		
		$this->set("createLinks", $this->buildPortfolioLinks($params, false, true));
		$this->set("div", $params["div"]);
	}

	function showFriendsActivity($params) {
		if(!isset($_SESSION["silk_user"]->params["id"])) {
			$link = SilkResponse::create_url(array(	"controller" => "homepage",
													"action" => "index"
													));
			redirect($link);
		}
		$params["limit"] = array(0,5);
		$friendsActivity = SocialController::getFriendsActivity($params);
		
		if(count($friendsActivity)) {
			$this->set("friends", 1);
			$this->set("friendsActivity", $friendsActivity);
		}

		if(!isset($params["div"])) $params["div"] = "FriendsActivity";
		$this->set("div", $params["div"]);
	}
	public function test($params) {
		$pPort = new PhotographerPortfolio();
		SilkGreg::barf("pPort", $pPort); die;
	}
	
	public function buildPortfolioLinks($params, $showExisting = true, $showMising = true) {
		$portfolios = MpController::get_user($_SESSION["silk_user"]->id)->get_portfolios();
		$ports = array();
		foreach($portfolios as $portfolio) {
			$ports[] = $portfolio->params["type"];
		}
		
		$usertypes = orm("UserType")->find_all_by_active(1);
		$createLinks = array();
		foreach($usertypes as $usertype) {
			if( (!in_array($usertype->class_name, $ports) && $showMising == true) ) {
				$createLinks[$usertype->name] = SilkResponse::create_url(array(	"controller" => "dashboard",
																				"action" => "createPortfolio",
																				"portfolioType" => $usertype->class_name
																				));
			}
			
			if(in_array($usertype->class_name, $ports) && $showExisting == true) {
				foreach($portfolios as $portfolio) {
					if($portfolio->params["type"] == $usertype->class_name) {
						$createLinks[$usertype->name] = SilkResponse::create_url(array(	"controller" => "mp",
																				"action" => "portfolio",
																				"id" => $portfolio->params["id"]
																				));
						break;
					}
				}
				
			}
		}
		return $createLinks;
	}
	
	public function createPortfolio($params) {
		$class_name = $params["portfolioType"];
		$portfolio = new $class_name;
		$portfolio->user_id = $_SESSION["silk_user"]->params["id"];
		$portfolio->name = "New Portfolio";
		$portfolio->about = "Enter description here...";
		$portfolio->experience_id = 2;
		$portfolio->height = 0;
		$portfolio->weight = 0;
		$portfolio->active = 0;
		$portfolio->save();
		redirect(SilkResponse::create_url(array("controller" => "dashboard")));
	}
	
	public function barf( $var, $name = "Barf" ) {
		echo humanize($name);
		echo "<pre>";
		var_dump( $var );
		echo "</pre>";
	}
}
?>